package com.xwh.shop.user.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xwh.shop.common.PageResult;
import com.xwh.shop.common.Result;
import com.xwh.shop.user.model.dto.MemberInfoPageQueryDTO;
import com.xwh.shop.user.model.entity.MemberInfo;
import com.xwh.shop.user.model.vo.MemberInfoPageVO;
import com.xwh.shop.user.service.service.MemberInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xwh
 * @since 2025-07-15
 */
@Slf4j
@RestController
@RequestMapping("/memberInfo")
@Tag(name = "会员信息管理")
public class MemberInfoController {

    @Resource
    private MemberInfoService memberInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @GetMapping("{id}")
    public Result<MemberInfo> getById(@PathVariable Long id){
        MemberInfo memberInfo = this.memberInfoService.getById(id);
        return Result.success(memberInfo);
    }

    @Operation(summary = "批量删除会员", description = "根据ID列表批量删除会员信息")
    @DeleteMapping()
    public Result<Object> delete(
            @Parameter(description = "会员ID列表", required = true)
            @RequestBody List<Long> ids) {
        this.memberInfoService.removeBatchByIds(ids);
        return Result.success();
    }
    @Operation(summary = "分页查询会员", description = "根据条件分页查询会员信息")
    @GetMapping("page")
    public PageResult<MemberInfoPageVO> page(
            @RequestParam(value = "current", defaultValue = "1") Long current,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "realName", required = false) String realName,
            @RequestParam(value = "email", required = false) String email
    ) {
        // 使用Builder模式创建DTO对象
        // 需要在MemberInfoPageQueryDTO中 添加@Builder注解
        MemberInfoPageQueryDTO queryDTO = MemberInfoPageQueryDTO.builder()
                .current(current)
                .pageSize(pageSize)
                .mobile(mobile)
                .status(status)
                .realName(realName)
                .email(email)
                .build();

        log.debug("分页查询会员信息，查询条件：{}", queryDTO);
        IPage<MemberInfoPageVO> page = this.memberInfoService.getPage(queryDTO);
        return PageResult.success(page);
    }


    @Operation(summary = "编辑会员信息", description = "根据ID编辑会员信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody MemberInfo member){
        this.memberInfoService.updateById(member);
        return Result.success();
    }

    @Operation(summary = "重置密码", description = "重置会员密码")
    @PutMapping("resetPassword/{id}")
    public Result<Object> resetPassword(@PathVariable Long id) {
        this.memberInfoService.resetPassword(id);
        return Result.success();
    }

    @Operation(summary = "启用禁用会员", description = "根据ID启用或禁用会员")
    @PutMapping("enable/{id}")
    public Result<Object> enable(@PathVariable Long id){
        MemberInfo memberInfo = this.memberInfoService.getById(id);
        this.memberInfoService.enable(id);
        return Result.success();
    }

}
